package com.muyu.danmo.service.impl;


import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoCommunity;
import com.muyu.danmo.domain.DanmoCommunityLike;
import com.muyu.danmo.mapper.DanmoCommunityLikeMapper;
import com.muyu.danmo.mapper.DanmoCommunityMapper;
import com.muyu.danmo.service.IDanmoCommunityLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 社区点赞Service业务处理层
 *
 * @author : kunzong
 * @date : 2024-11-19
 */
@Service
public class DanmoCommunityLikeServiceImpl implements IDanmoCommunityLikeService {

    @Autowired
    private DanmoCommunityLikeMapper danmoCommunityLikeMapper;

    @Autowired
    private DanmoCommunityMapper danmoCommunityMapper;

    @Override
    public DanmoCommunityLike saveLike(DanmoCommunityLike like) {
        danmoCommunityLikeMapper.insert(like);
        return like;
    }

    @Override
    public boolean deleteLikeById(int id) {
        return danmoCommunityLikeMapper.deleteById(id) > 0;
    }

    @Override
    public List<DanmoCommunityLike> getLikesByPostId(int postId) {
        return danmoCommunityLikeMapper.selectByPostId(postId);
    }

    @Override
    public List<DanmoCommunityLike> getLikesByUserId(Long userId) {
        return danmoCommunityLikeMapper.selectByUserId(userId);
    }

    @Override
    public DanmoCommunityLike getLikeById(int id) {
        return danmoCommunityLikeMapper.selectById(id);
    }

    /**
     * 判断用户是否已点赞
     *
     * @param id
     * @return
     */
    @Override
    @Transactional // 确保方法是事务性的
    public synchronized Result isCollect(Long id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        // 开始事务
        try {
            List<DanmoCommunityLike> danmoCommunityLikeList = danmoCommunityLikeMapper.selectDanmoCollectMaterialByIdAndUserId(id, loginUser.getUserid());
            DanmoCommunityLike danmoCommunityLike;
            if (danmoCommunityLikeList.size() == 0) {
                // 用户未点赞，进行点赞操作
                danmoCommunityLike = new DanmoCommunityLike();
                danmoCommunityLike.setUserId(loginUser.getUserid());
                danmoCommunityLike.setPostId(id);
                danmoCommunityLike.setCreateTime(DateUtils.getNowDate());
                danmoCommunityLike.setLikeName("true"); // 可以考虑使用常量
                danmoCommunityLikeMapper.insertDanmoCommunityLike(danmoCommunityLike);

                // 更新社区的浏览次数
                DanmoCommunity danmoCommunity = danmoCommunityMapper.selectDanmoCommunityById(id);
                if (danmoCommunity == null) {
                    return Result.error("帖子不存在");
                }
                // 确保视图计数的初始化
                if (danmoCommunity.getViews() == null) {
                    danmoCommunity.setViews(0L);
                }
                danmoCommunity.setViews(danmoCommunity.getViews() + 1);
                danmoCommunityMapper.updateDanmoCommunity(danmoCommunity);

                return Result.success(id, "点赞成功");
            } else {
                // 用户已点赞，进行取消点赞
                for (DanmoCommunityLike communityLike : danmoCommunityLikeList) {
                    danmoCommunityLikeMapper.deleteDanmoCollectMaterialById(communityLike.getId());
                }
                return Result.success(1, "取消点赞成功");
            }
        } catch (Exception e) {
            // 处理异常
            return Result.error("操作失败，错误信息：" + e.getMessage());
        }
    }
}